from traits.api import *
from traitsui.api import *
from threading import Thread
from time import sleep
from traitsui.menu import NoButtons
import numpy as np

if __name__ == "__main__":
    import Tree_editor
    import Table_with_progress
    import Table_with_checkbox
    import List_selection
    import Dynamic_enum
    import Dynamic_range
    import LED_display
    import Table_editor
else:
    from . import Tree_editor
    from . import Table_with_progress
    from . import Table_with_checkbox
    from . import List_selection
    from . import Dynamic_enum
    from . import Dynamic_range
    from . import LED_display
    from . import Table_editor

class Multi_thread(HasTraits):
    # The thread specific counters:
    thread_0 = Int()
    thread_1 = Int()
    thread_2 = Int()
    # The button used to start the threads running:
    start = Button('Start Threads')
    # The count of how many threads ae currently running:
    running = Int()
    view = View(
        VGroup(
            Item('thread_0', style='readonly'),
            Item('thread_1', style='readonly'),
            Item('thread_2', style='readonly'),
        ),
        '_',
        Item('start', show_label=False, enabled_when='running == 0'),
        resizable=True,
        width=250,
        title='Monitoring threads',
    )
    def _start_changed(self):
        for i in range(3):
            Thread(
                target=self.counter,
                args=('thread_%d' % i, (i * 10 + 10) / 1000.0),
            ).start()
    def counter(self, name, interval):
        self.running += 1
        count = 0
        for i in range(200):
            setattr(self, name, count)
            count += 1
            sleep(interval)
        self.running -= 1
demo = Multi_thread()

class Invalid_handling(HasTraits):

    # The mass of the system:
    mass = Range(0.0, 100.0)
    # The velocity of the system:
    velocity = Range(0.0, 100.0)
    # The kinetic energy of the system:
    kinetic_energy = Property(Float, observe='mass, velocity')
    # The current error status of the system:
    error = Property(
        Bool,
        sync_to_view='mass.invalid, velocity.invalid, status.invalid',
        observe='kinetic_energy',
    )
    # The current status of the system:
    status = Property(Str, observe='error')
    view = View(
        VGroup(
            VGroup(
                Item('mass'),
                Item('velocity'),
                Item('kinetic_energy', style='readonly', format_str='%.0f'),
                label='System',
                show_border=True,
            ),
            VGroup(
                Item('status', style='readonly', show_label=False),
                label='Status',
                show_border=True,
            ),
        )
    )
    def _get_kinetic_energy(self):
        return (self.mass * self.velocity * self.velocity) / 2.0
    def _get_error(self):
        return self.kinetic_energy > 50000.0
    def _get_status(self):
        if self.error:
            return 'The kinetic energy of the system is too high.'
        return ''
demo = Invalid_handling()

class History_editor(HasTraits):
    name = Str()
    file = File()
    directory = Directory()
    view = View(
        Item('name', id='name', editor=HistoryEditor(entries=5)),
        Item('file', id='file1', editor=FileEditor(entries=10)),
        Item(
            'file',
            id='file2',
            editor=FileEditor(
                entries=10,
                filter=['All files (*.*)|*.*', 'Python files (*.py)|*.py'],
            ),
        ),
        Item('directory', id='directory', editor=DirectoryEditor(entries=10)),
        title='History Editor Demo',
        id='enthought.test.history_demo.HistoryDemo',
        width=0.33,
        resizable=True,
    )
demo = History_editor()

button = Item('ignore', show_label=False)
class Using_spring(HasTraits):
    ignore = Button('Ignore')
    view = View(
        VGroup(
            '10',
            Label(label='Spring in a horizontal group moves widget right:'),
            '10',
            HGroup(
                button,
                button,
                show_border=True,
                label='Left justified (no springs)',
            ),
            HGroup(
                spring,
                button,
                button,
                show_border=True,
                label='Right justified with a spring ' 'before any buttons',
            ),
            HGroup(
                button,
                spring,
                button,
                show_border=True,
                label='Left and right justified with a '
                'spring between buttons',
            ),
            HGroup(
                button,
                button,
                spring,
                button,
                button,
                spring,
                button,
                button,
                show_border=True,
                label='Left, center and right justified '
                'with springs after the 2nd and 4th '
                'buttons',
            ),
            spring,
            Label(
                'Spring in vertical group moves widget down '
                '(does not work on Wx backend).'
            ),
            button,
        ),
        width=600,
        height=600,
        resizable=True,
        title='Spring Demo',
        buttons=['OK'],
    )
demo = Using_spring()

class Array_editor(HasPrivateTraits):
    three = Array(np.int64, (3, 3))
    four = Array(
        np.float64,
        (4, 4),
        editor=ArrayEditor(width=-50),
    )
    view = View(
        Item('three', label='3x3 Integer'),
        '_',
        Item('three', label='Integer Read-only', style='readonly'),
        '_',
        Item('four', label='4x4 Float'),
        '_',
        Item('four', label='Float Read-only', style='readonly'),
        buttons=NoButtons,
        resizable=True,
    )
demo = Array_editor()

class Check_list(HasTraits):
    """Define the main CheckListEditor demo class."""
    # Define a trait for each of three formations:
    checklist_4col = List(
        editor=CheckListEditor(values=['one', 'two', 'three', 'four'], cols=4)
    )
    checklist_2col = List(
        editor=CheckListEditor(values=['one', 'two', 'three', 'four'], cols=2)
    )
    checklist_1col = List(
        editor=CheckListEditor(values=['one', 'two', 'three', 'four'], cols=1)
    )
    # CheckListEditor display with four columns:
    cl_4_group = Group(
        Item('checklist_4col', style='simple', label='Simple'),
        Item('_'),
        Item('checklist_4col', style='custom', label='Custom'),
        Item('_'),
        Item('checklist_4col', style='text', label='Text'),
        Item('_'),
        Item('checklist_4col', style='readonly', label='ReadOnly'),
        label='4-column',
    )
    # CheckListEditor display with two columns:
    cl_2_group = Group(
        Item('checklist_2col', style='simple', label='Simple'),
        Item('_'),
        Item('checklist_2col', style='custom', label='Custom'),
        Item('_'),
        Item('checklist_2col', style='text', label='Text'),
        Item('_'),
        Item('checklist_2col', style='readonly', label='ReadOnly'),
        label='2-column',
    )
    # CheckListEditor display with one column:
    cl_1_group = Group(
        Item('checklist_1col', style='simple', label='Simple'),
        Item('_'),
        Item('checklist_1col', style='custom', label='Custom'),
        Item('_'),
        Item('checklist_1col', style='text', label='Text'),
        Item('_'),
        Item('checklist_1col', style='readonly', label='ReadOnly'),
        label='1-column',
    )
    # The view includes one group per column formation.  These will be
    # displayed on separate tabbed panels.
    traits_view = View(
        cl_4_group,
        cl_2_group,
        cl_1_group,
        title='CheckListEditor',
        buttons=['OK'],
        resizable=True,
    )
demo = Check_list()

class Color_editor(HasTraits):
    color_trait = Color()
    color_group = Group(
        Item('color_trait', style='simple', label='Simple'),
        Item('_'),
        Item('color_trait', style='custom', label='Custom'),
        Item('_'),
        Item('color_trait', style='text', label='Text'),
        Item('_'),
        Item('color_trait', style='readonly', label='ReadOnly'),
    )
    traits_view = View(
        color_group, title='ColorEditor', buttons=['OK'], resizable=True
    )
demo = Color_editor()

class Font_editor(HasTraits):
    # Define a Font trait to view:
    my_font_trait = Font()
    # Display specification (one Item per editor style):
    font_group = Group(
        Item('my_font_trait', style='simple', label='Simple'),
        Item('_'),
        Item('my_font_trait', style='custom', label='Custom'),
        Item('_'),
        Item('my_font_trait', style='text', label='Text'),
        Item('_'),
        Item('my_font_trait', style='readonly', label='ReadOnly'),
    )
    traits_view = View(
        font_group, title='FontEditor', buttons=['OK'], resizable=True
    )
demo = Font_editor()

class Set_editor(HasTraits):
    """Defines the SetEditor demo class."""
    # Define a trait each for four SetEditor variants:
    unord_nma_set = List(
        editor=SetEditor(
            values=['kumquats', 'pomegranates', 'kiwi'],
            left_column_title='Available Fruit',
            right_column_title='Exotic Fruit Bowl',
            can_move_all=False,
        )
    )
    unord_ma_set = List(
        editor=SetEditor(
            values=['kumquats', 'pomegranates', 'kiwi'],
            left_column_title='Available Fruit',
            right_column_title='Exotic Fruit Bowl',
        )
    )
    ord_nma_set = List(
        editor=SetEditor(
            values=['apples', 'berries', 'cantaloupe'],
            left_column_title='Available Fruit',
            right_column_title='Fruit Bowl',
            ordered=True,
            can_move_all=False,
        )
    )
    ord_ma_set = List(
        editor=SetEditor(
            values=['apples', 'berries', 'cantaloupe'],
            left_column_title='Available Fruit',
            right_column_title='Fruit Bowl',
            ordered=True,
        )
    )
    # SetEditor display, unordered, no move-all buttons:
    no_nma_group = Group(
        Item('unord_nma_set', style='simple'),
        label='Unord I',
        show_labels=False,
    )
    # SetEditor display, unordered, move-all buttons:
    no_ma_group = Group(
        Item('unord_ma_set', style='simple'),
        label='Unord II',
        show_labels=False,
    )
    # SetEditor display, ordered, no move-all buttons:
    o_nma_group = Group(
        Item('ord_nma_set', style='simple'), label='Ord I', show_labels=False
    )
    # SetEditor display, ordered, move-all buttons:
    o_ma_group = Group(
        Item('ord_ma_set', style='simple'), label='Ord II', show_labels=False
    )
    # The view includes one group per data type. These will be displayed
    # on separate tabbed panels:
    traits_view = View(
        no_nma_group,
        no_ma_group,
        o_nma_group,
        o_ma_group,
        title='SetEditor',
        buttons=['OK'],
    )
demo = Set_editor()

class Form(HasTraits):
    Tree_editor = Instance(Tree_editor.demo, ())
    Table_with_progress = Instance(Table_with_progress.demo, ())
    Table_with_checkbox = Instance(Table_with_checkbox.demo, ())
    Multi_thread = Instance(Multi_thread, ())
    List_selection = Instance(List_selection.demo, ())
    Invalid_handling = Instance(Invalid_handling, ())
    History_editor = Instance(History_editor, ())
    Dynamic_enum = Instance(Dynamic_enum.demo, ())
    Dynamic_range = Instance(Dynamic_range.demo, ())
    LED_display = Instance(LED_display.demo, ())
    Using_spring = Instance(Using_spring, ())
    Array_editor = Instance(Array_editor, ())
    Check_list = Instance(Check_list, ())
    Color_editor = Instance(Color_editor, ())
    Font_editor = Instance(Font_editor, ())
    Set_editor = Instance(Set_editor, ())
    Table_editor = Instance(Table_editor.demo, ())

    traits_view=View(
        Tabbed(
            UItem('Tree_editor',style='custom'),
            UItem('Table_with_progress', style='custom'),
            UItem('Table_with_checkbox', style='custom'),
            UItem('Multi_thread', style='custom'),
            UItem('List_selection', style='custom'),
            UItem('Invalid_handling', style='custom'),
            UItem('History_editor', style='custom'),
            UItem('Dynamic_enum', style='custom'),
            UItem('Dynamic_range', style='custom'),
            UItem('LED_display', style='custom'),
            UItem('Using_spring', style='custom'),
            UItem('Array_editor', style='custom'),
            UItem('Check_list', style='custom'),
            UItem('Color_editor', style='custom'),
            UItem('Font_editor', style='custom'),
            UItem('Set_editor', style='custom'),
            UItem('Table_editor', style='custom'),
        ),
        resizable=True,
    )

if __name__ == "__main__":
    Form().configure_traits()